-- rummage_soul
-- create by wangy
-- 翻找尸体的灵魂

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    trigger = function(who, prop, arg)
        local grid = arg.grid;
        local class = grid.class;
        local dbase = DungeonM.query(class, "dbase");
        local bonusList = {};
        local prefix = "formula_";

        if type(dbase.soul_item) == "table" then
            bonusList = dbase.soul_item;
        elseif type(dbase.soul_item) == "string" and
               string.startWith(dbase.soul_item, prefix) then
            local formulaName = string.sub(dbase.soul_item, string.len(prefix) + 1);
            bonusList = FormulaM.invoke(formulaName, dbase.random_bonus);
        end

        if type(bonusList) ~= "table" or #bonusList == 0 then
            return false;
        end

        -- 如果只有一种奖励，需要转换一下格式
        if type(bonusList[1]) == "number" then
            bonusList = { bonusList };
        end

        for _, bonus in pairs(bonusList) do
            BonusM.doBonus(bonus, "rummage_soul");
        end

        -- 抛出翻找灵魂事件
        EventMgr.fire(event.RUMMAGE_SOUL, { ["bonus"] = bonusList, ["pos"] = grid:getPos()});

        return true;
    end,

    record = function(target, prop, path)
        return false;
    end
};